<?php 
class kdtrace_toll extends Controller {
	
	function track( $tracknum , $lang = "en"){
		
		$getdata = '{"trackconnote":"' . $tracknum . '","viewtype":"aview","stid":"toll","OptionalField":""}';
		$url 	 = "https://ws01.ffdx.net/toll/etrack_blank.aspx/WS_Header_SearchTrack_Extended2" ;
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $getdata);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(
			'Content-Type: application/json',
			'Referer: https://ws01.ffdx.net/toll/etrack_blank.aspx?stid=toll&ifr=y&cn=&t=&version=toll',
			'User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36' 
		));
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
		$result = curl_exec($ch);
        curl_close($ch);
		$rtndata = array('tracknum'=>$tracknum,'header'=>array(),'err'=>array(),'eventList'=>array());
		$result = json_decode ($result);
		$result = $result->d;

		if( strpos($result , "No tracking information exists")>0 ){
			$rtndata['err']['errcode'] = '1';
			return $rtndata;
		}
		$rtndata['err']['errcode'] = '0';
		$rhtm = str_replace("&nbsp;" , " " , $result);
		
		if(preg_match("/>Destination <\/td><td valign\=\'top\'>:<\/td><td class\=\'Size9\'>(.+?)<\/td>/i", $rhtm, $matches) == 1){
			$rtndata['header']['shipFrom']		= $matches[1];
		};
		if(preg_match("/>Status <\/td><td valign\=\'top\'>:<\/td><td class\=\'Size9\'>(.+?)</i", $rhtm, $matches) == 1){
			$rtndata['header']['keyStatus']			= trim($matches[1]);
		};
		if(preg_match("/\'>Delivered On :  <strong>(.+?)<\/strong><br>Signed By : <strong>(.+?)<\/strong>/i", $rhtm, $matches) == 1){
			$rtndata['header']['SignedBy']			= trim($matches[2]);
			$matches[1]!="" ? $rtndata['header']['isDelivered']=true : $rtndata['header']['isDelivered']=false;
		};

		$w1 = strpos($rhtm , "id='trackresult'" );
		$w2 = strpos($rhtm , '</table>' ,$w1);
		$bdhtm = substr($rhtm , $w1 , $w2 - $w1);
		preg_match_all('/<tr><td>(.+?)<\/td><td>(.+?)<\/td><td class\=\'trackeventid\' value\="\d*">(.+?)<\/td><t(?:.+?)>(.+?)<\/td><\/tr>/i', $bdhtm, $matches);

		$count = count($matches[0]);
		if($count>0){
			for($i=0 ; $i < $count ; $i++){
				//$this->skipSpaces($matches[1][$i])
				$tt =  strtotime( $this->skipSpaces($matches[1][$i]) . " " . $this->skipSpaces($matches[2][$i]) );
				$rtndata['eventList'][$i] = array(
					"date"			=> date('Y-m-d',$tt),
					"time"			=> date('h:i:s',$tt),
					"utime"			=> $tt,
					"status"		=> $this->skipSpaces($matches[4][$i]),
					"location"		=> $this->skipSpaces($matches[3][$i])
				);
			}
		}

		$rtndata['eventList'] = array_reverse( $rtndata['eventList'] );
		return $rtndata;
	}
	function skipSpaces($t){
			return  trim(preg_replace('/\s{2,}/', ' ', $t));
	}
	
}

?>